<?php

namespace app\api\controller;

use app\api\model\Leader as LeaderModel;
use app\common\model\leader\Category as CategoryModel;

/**
 * 团队控制器
 * Class Leader
 * @package app\api\controller
 */
class Leader extends Controller
{
    private $user;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->user = $this->getUser();
    }

    /**
     *
     * @return array
     */
    public function index()
    {
        // 领导团队列表
        $categoryList = CategoryModel::where("shanghui_id", $this->user['shanghui_id'])->order("sort asc")->select();
        return $this->renderSuccess(compact('categoryList'));
    }

    /**
     * 团队列表
     * @param int $category_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists($searchInput = "", $category_id = 0)
    {
        $model = new LeaderModel;
        $list = $model->getList($searchInput,$category_id,1000,$this->user['shanghui_id']);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 团队详情
     * @param $Leader_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail($leader_id)
    {
        $detail = LeaderModel::detail($leader_id);
        return $this->renderSuccess(compact('detail'));
    }

    /****************************************************************************
     * 添加团队分类
     * @param $Leader_id
     */
    public function addCate()
    {
       $param = input('param.');
       $data =[
            'name'=>$param['cate_name'],
            'sort'=>$param['cate_sort'],
            'shanghui_id'=>$this->user['shanghui_id'],
            'create_time'=>time(),
            'update_time'=>time(),
            'wxapp_id'=>$param['wxapp_id'],
       ];
       CategoryModel::insert($data);
       return $this->renderSuccess([],'分类添加成功');
    }
     /****************************************************************************
     * 编辑团队分类
     * @param $Leader_id
     */
    public function editCate()
    {
        $param = input('param.');
        $data =[
             'name'=>$param['cate_name'],
             'sort'=>$param['cate_sort'],
             'update_time'=>time(),
        ];
        CategoryModel::where('shanghui_id',$this->user['shanghui_id'])->where('category_id',$param['category_id'])->update($data);
        return $this->renderSuccess([],'分类修改成功');
    }
    /****************************************************************************
     * 添加团队领导
     * @param $Leader_id
     */
    public function add()
    {
        $param = input('param.');
        $data =[
             'name'=>$param['name'],
             'company'=>$param['company'],
             'duties'=>$param['duties'],
             'position'=>$param['position'],
             'category_id'=>$param['category_id'],
             'logo_image_id'=>$param['logo_image_id'],
             'shanghui_id'=>$this->user['shanghui_id'],
             'wxapp_id'=>$this->user['wxapp_id'],
             'sort'=>$param['sort'],
             'update_time'=>time(),
             'create_time'=>time(),
        ];
        LeaderModel::insert($data);
        return $this->renderSuccess([],'团队领导添加成功');
    }
    /****************************************************************************
     * 编辑团队领导
     * @param $Leader_id
     */
    public function edit()
    {
        $param = input('param.');
        $data =[
             'name'=>$param['name'],
             'company'=>$param['company'],
             'duties'=>$param['duties'],
             'position'=>$param['position'],
             'category_id'=>$param['category_id'],
             'sort'=>$param['sort'],
             'logo_image_id'=>$param['logo_image_id'],
             'update_time'=>time(),
        ];
        LeaderModel::where('shanghui_id',$this->user['shanghui_id'])->where('id',$param['id'])->update($data);
        return $this->renderSuccess([],'团队领导修改成功');
    }

}
